package com.zq.sdklib

import com.android.build.gradle.AppExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.zq.sdklib.extension.PluginExtension
import com.zq.sdklib.task.PrintTask
import com.zq.sdklib.transform.cost.asm.AsmMethodCostTransform
import com.zq.sdklib.transform.cost.javassist.MethodCostTransform
import org.gradle.api.Plugin
import org.gradle.api.Project

public class PluginImpl implements Plugin<Project> {


    void apply(Project project) {
        LogUtil.i("apply")
        addExtensions(project)
        addTask(project)
        //重命名输出文件
        renameApk(project)
        //添加 依赖库
        adddependencies(project)
        //transform api， aop 过程
        addTransform(project)
    }

    //定义扩展属性
    //project.extensions返回ExtensionContainer对象，
    // project.extensions.create("Name", ExtensionClass)创建 extension,
    // 按后面的使用来看，创建extension后会将其加入到ExtensionContainer对象中
    void addExtensions(Project project){
        //创建扩展属性 ZQSDK，并将外部属性配置使用PluginExtension进行管理
        project.extensions.create(Constant.CONFIG_NAME, PluginExtension, project)
    }

    void addTask(Project project){
        project.task('zqPrintTask', type: PrintTask)
        project.task('zqPrintDependencies').doLast {
            LogUtil.i("$it")
            LogUtil.i("plugins:----------------------")
            project.plugins.each {
                LogUtil.i("plugin : ${it.toString()}")
            }
            LogUtil.i("configurations:----------------------")
            project.configurations.each {
                LogUtil.i("configuration:$it.name")
                it.getAllDependencies().each {
                    LogUtil.i("$it.group:$it.name:$it.version")
                }
            }
        }
        project.tasks.findByName('zqPrintDependencies').group = Constant.GROUP
    }

    //修改apk 文件名称
    void renameApk(Project project){
        //配置完成后回调,
        BaseAppModuleExtension android = project.findProperty("android")
        android.applicationVariants.all { variant ->
            LogUtil.i("变体 -- ${variant.name}")
            variant.outputs.all { output ->
                def origin = outputFileName
                // 定义apk的名字，拼接variant的版本号
                def apkName = "${variant.versionName}-${StringUtil.getBuildTime()}"
                // 判断是否为空
                if (!variant.flavorName.isEmpty()) {
                    apkName += "-${variant.flavorName}"
                }
                if (!variant.buildType.name.isEmpty()) {
                    apkName += "-${variant.buildType.name}"
                }
                // 赋值属性
                outputFileName = apkName + ".apk"
                LogUtil.i("outputFileName : ${origin} => ${outputFileName}")
            }
        }
//        project.android.applicationVariants.all{ variant ->
//
//        }
    }

    void adddependencies(Project project){
        //配置完成后回调 ，project配置成功
        project.afterEvaluate {
            LogUtil.i("afterEvaluate of ${project.path}")
            if(project.(Constant.CONFIG_NAME)){
                if(project.(Constant.CONFIG_NAME).testDomains){
                    project.(Constant.CONFIG_NAME).testDomains.all { data ->
                        LogUtil.i "$data"
                    }
                }

                if(project.(Constant.CONFIG_NAME).baseLib){
                    String baseLibName = "com.gitee.zaiqiang231:base-lib:${project.(Constant.CONFIG_NAME).baseLib.versionName}"
                    LogUtil.i("依赖 $baseLibName")
                    project.dependencies.add("api",baseLibName)
                }
                if(project.(Constant.CONFIG_NAME).customViewLib){
                    String customName = "com.gitee.zaiqiang231:base-customview:${project.(Constant.CONFIG_NAME).customViewLib.versionName}"
                    LogUtil.i("依赖 $customName")
                    project.dependencies.add("api", customName)
                }
            }
        }
    }

    void addTransform(Project project){
        AppExtension android =  project.getExtensions().getByType(AppExtension.class)
//        android.registerTransform(new MethodCostTransform(project))
        android.registerTransform(new AsmMethodCostTransform(project))
    }
}